import moment from "moment"

function MoneyFormat(value: number | string | undefined, precision = 2, separator = ',') {
  const str = `${value}`.replace(/,/g, '')
  const money = parseFloat(`${str}`) || 0  
  const moneyStr = (Math.abs(money) || 0).toFixed(precision)
  const mark = money < 0 ? '-' : ''
  return mark + moneyStr.replace(new RegExp(`(?!^)(?=(\\d{3})+${moneyStr.includes('.') ? '\\.' : '$'})`, 'g'), separator)
}

function TimeFormart(value: string | number | undefined) {
  if (!value && value != 0) {
    return '-'
  } else {
    const now = moment()
    const formatTime = moment(value)
    const timeDate = formatTime.date()
    const timeMinutes = formatTime.valueOf() / (1000 * 60)
    const nowDate = now.date()
    const nowMinutes = now.valueOf() / (1000 * 60)

    if (timeDate === nowDate) {
      if (nowMinutes - timeMinutes < 60) {
        return `${Math.round(nowMinutes - timeMinutes)}分钟前`
      }
      return `今天 ${formatTime.format('HH:mm')}`
    }
    if (nowDate - timeDate === 1) {
      return `昨天 ${formatTime.format('HH:mm')}`
    }
    if (nowDate - timeDate === 2) {
      return `前天 ${formatTime.format('HH:mm')}`
    }
    return formatTime.format('YYYY-MM-DD HH:mm')
  }
}

export {
  MoneyFormat,
  TimeFormart
}
